package com.ruoyi.web.controller.word;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.RandomUtil;
import com.ruoyi.common.utils.baidu.TransApi;
import com.ruoyi.common.domain.word.BaiduWord;
import com.ruoyi.common.domain.word.EcDict;
import com.ruoyi.word.service.BaiduWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/baidu/word")
public class BaiduWordController extends BaseController {

    @Autowired
    private BaiduWordService biz;

//    @Autowired
//    private ICommonSubjectService subjectService;

    @Autowired
    private BaiduWordExtController extController;

    @PostMapping("/save")
    public BaiduWord saveObj(@RequestParam(defaultValue = "a", required = false) String query) {
        try {
            BaiduWord w = biz.getByName(query);
            if (w != null) {
                System.out.println("数据已存在: "+ query);
                return w;
            }
            JSONObject data = TransApi.trans(query);
            if (!data.containsKey("trans_result")) {
                System.out.println("单词翻译失败: "+ query);
                return null;
            }
            BaiduWord word = new BaiduWord();
            word.setId(RandomUtil.getRandomNum(8));
            word.setFrom(data.getString("from"));
            word.setTo(data.getString("to"));
            JSONObject trans_result = data.getJSONArray("trans_result").getJSONObject(0);
            word.setSrc(trans_result.getString("src"));
            word.setDst(trans_result.getString("dst"));
            word.setSrc_tts(trans_result.getString("src_tts"));
            word.setDst_tts(trans_result.getString("dst_tts"));
            word.setDict(trans_result.getJSONObject("dict"));
            biz.saveObj(word);
            return word;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

//    @GetMapping("/saveAll")
//    public String saveAll() {
//        List<Words> allWords = biz.findAllWords();
//        allWords.forEach(word -> {
//            BaiduWord w = saveObj(word.getWord());
//            if(w != null) {
//                System.out.println("pull success: " + word.getWord());
//            }
//        });
//        return "保存成功";
//    }

    @GetMapping("/saveAllFromEc")
    public String saveAllFromEc() {
        List<EcDict> allWords = biz.findAllEcDict();
        allWords.forEach(word -> {
            BaiduWord w = saveObj(word.getWord());
            if(w != null) {
                System.out.println("pull success: " + word.getWord());
            }
        });
        return "保存成功";
    }

//    @GetMapping("/saveAllFromSubject")
//    public String saveAllFromSubject() {
//        List<CommonSubject> subjects = subjectService.findAll();
//        subjects.stream().forEach(sub -> {
//            CommonSubject subject = subjectService.findById(sub.getId());
//            HashSet<String> allWords = StringUtils.getAllWords(subject.getContent());
//            allWords.stream().forEach(word -> {
//                BaiduWord w = saveObj(word);
//                if(w != null) {
//                    System.out.println("pull success: " + word);
//                }
//            });
//        });
//        return "保存成功";
//    }




    @GetMapping("/findAll")
    public List<BaiduWord> findAll() {
        return biz.findAll();
    }

    @GetMapping("/findOne")
    public BaiduWord findOne(@RequestParam String id) {
        return biz.getById(id);
    }

    @GetMapping("/findOneByName")
    public BaiduWord findOneByName(@RequestParam String name) {
        BaiduWord word = biz.getByName(name);
        if(word == null) {
            BaiduWord w = saveObj(name);
            extController.saveObjSync(name);
            return w;
        }
        return word;
    }

    @PostMapping("/update")
    public String update(@RequestBody BaiduWord book) {
        return biz.updateBook(book);
    }

    @PostMapping("/delOne")
    public String delOne(@RequestBody BaiduWord book) {
        return biz.deleteBook(book);
    }

    @GetMapping("/delById")
    public String delById(@RequestParam String id) {
        return biz.deleteById(id);
    }

    @GetMapping("/findlikes")
    public List<BaiduWord> findByLikes(@RequestParam String search) {
        return biz.findByLikes(search);
    }

    @GetMapping("/removeDuplicateData")
    public String removeDuplicateData(){
        return biz.removeDuplicateData();
    }
}